var t = (0, require("../../../@babel/runtime/helpers/interopRequireDefault").default)(require("../../../utils/qqmap"));
new(require("../../../utils/http").HTTP);
Page({
  data: {
    now_time: "",
    current_address: "",
    status: 0,
    now_time_stop: "",
    imgUrl: ""
  },
  onLoad: function (t) {
    this.getCurrentTime(),
      this.getLocation(),
      this.setData({
        now_time: this.getTime()
      }),
      this.getParams()
  },
  getParams: function () {
    var t = this;
    wx.request({
      url: "https://www.qlsyyun.com:3030/getParams",
      success: function (e) {
        t.setData({
          ossSignature: e.data.signature,
          ossAccessKeyId: e.data.OSSAccessKeyId,
          ossPolicy: e.data.policy
        })
      },
      fail: function (t) {
        console.log("请求签名失败：", t)
      }
    })
  },
  selectFile: function () {
    var t = this;
    wx.chooseMedia({
      count: 1,
      mediaType: ["image"],
      sourceType: ["album", "camera"],
      success: function (e) {
        var n = e.tempFiles;
        if (n[0].size > 10485760)
          wx.showToast({
            title: "文件大小不能超过10M",
            icon: "none",
            duration: 3e3
          });
        else {
          var s = new Date,
            o = "photo/" + s.getFullYear() + "-" + (s.getMonth() + 1) + "/" + Date.parse(new Date) + parseInt(90001 * Math.random() + 1e4, 10) + ".jpg";
          t.setData({
            key: o,
            tempFilePaths: n[0],
            imgUrl: n[0].tempFilePath
          })
        }
      }
    })
  },
  upload: function (t, e) {
    wx.showLoading({
        title: "上传中...",
        icon: "loading",
        mask: !0
      }),
      wx.uploadFile({
        url: "https://qlsy.oss-cn-hangzhou.aliyuncs.com",
        filePath: e,
        name: "file",
        formData: {
          key: t,
          policy: this.data.ossPolicy,
          OSSAccessKeyId: this.data.ossAccessKeyId,
          signature: this.data.ossSignature
        },
        success: function (e) {
          204 === e.statusCode && (console.log("访问地址：", "http://qlsy.oss-cn-hangzhou.aliyuncs.com/" + t), wx.setStorageSync("address", "https://qlsy.oss-cn-hangzhou.aliyuncs.com/" + t), wx.showToast({
            title: "提交成功",
            icon: "none",
            duration: 1e3,
            mask: !0,
            success: function () {
              setTimeout((function () {
                wx.navigateBack({
                  delta: 1
                })
              }), 1e3)
            }
          }))
        },
        fail: function (t) {
          console.log("上传失败：", t),
            wx.showLoading({
              title: "上传失败，请重试",
              icon: "loading",
              mask: !0
            })
        },
        complete: function () {
          wx.hideLoading()
        }
      })
  },
  clockInStart: function () {
    return this.data.imgUrl ? this.data.current_address ? (wx.vibrateLong(), this.setData({
      status: 1,
      now_time_stop: this.data.now_time
    }, wx.showToast({
      title: "打卡成功",
      icon: "none"
    })), wx.setStorageSync("clock", this.data.now_time), wx.setStorageSync("orient", this.data.current_address), void this.upload(this.data.key, this.data.tempFilePaths.tempFilePath)) : wx.showToast({
      title: "未获取当前定位",
      icon: "error"
    }) : wx.showToast({
      title: "尚未上传打卡照",
      icon: "error"
    })
  },
  getCurrentTime: function () {
    var t = this;
    setInterval((function () {
      t.setData({
        now_time: t.getTime()
      })
    }), 1e3)
  },
  getTime: function () {
    return (new Date).getHours() + ":" + ((new Date).getMinutes() < 10 ? "0" + (new Date).getMinutes() : (new Date).getMinutes()) + ":" + ((new Date).getSeconds() < 10 ? "0" + (new Date).getSeconds() : (new Date).getSeconds())
  },
  getUserAuth: function () {
    var t = this;
    return new Promise((function (e, n) {
      wx.authorize({
        scope: "scope.userLocation"
      }).then((function () {
        e()
      })).catch((function () {
        var e = t;
        wx.getSetting({
          success: function (t) {
            null != t.authSetting["scope.userLocation"] && 1 != t.authSetting["scope.userLocation"] ? wx.showModal({
              title: "请求授权当前位置",
              content: "需要获取您的地理位置，请确认授权",
              success: function (t) {
                t.cancel ? wx.showToast({
                  title: "拒绝授权",
                  icon: "none",
                  duration: 1e3
                }) : t.confirm && wx.openSetting({
                  success: function (t) {
                    1 == t.authSetting["scope.userLocation"] ? e.getLocation() : wx.showToast({
                      title: "授权失败",
                      icon: "none",
                      duration: 1e3
                    })
                  }
                })
              }
            }) : (t.authSetting["scope.userLocation"], e.getLocation())
          }
        })
      }))
    }))
  },
  getLocation: function () {
    var e = this;
    wx.getLocation({
      type: "gcj02",
      success: function (n) {
        var s = n.longitude,
          o = n.latitude,
          a = o + "," + s;
        e.translate(a).then((function (t) {
            t.locations && (o = t.locations[0].lat, s = t.locations[0].lng)
          })),
          t.default.reverseGeocoder({
            location: {
              latitude: o,
              longitude: s
            },
            success: function (t) {
              var n = t.result.address + t.result.formatted_addresses.recommend;
              e.setData({
                current_address: n
              })
            },
            fail: function (t) {
              console.log(t)
            }
          })
      },
      fail: function (t) {
        console.log(t),
          e.getUserAuth(),
          wx.showToast({
            title: "获取定位失败，请打开手机定位，重新进入！",
            icon: "none"
          })
      }
    })
  },
  refreshAdd: function () {
    this.getLocation()
  },
  translate: function (t) {
    return new Promise((function (e, n) {
      wx.request({
        url: "https://apis.map.qq.com/ws/coord/v1/translate",
        method: "GET",
        data: {
          locations: t,
          type: 5,
          key: "CREBZ-B7PKX-GL44O-7ITDB-7UFU7-OLFDV"
        },
        success: function (t) {
          return e(t.data)
        },
        fail: function (t) {
          return n(t)
        }
      })
    }))
  },
  deleteImg: function (t) {
    this.setData({
      imgUrl: ""
    })
  },
  previewImg: function (t) {
    wx.previewImage({
      current: this.data.imgUrl,
      urls: [this.data.imgUrl]
    })
  }
});